package com.ggkt.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @desc   日期工具类
 * @author ggkt
 * @date   2021-04-26
 */
public class DateUtil {

    public static void main(String[] args) {
        System.out.println("today's String is: "+DateUtil.dateToString(new Date(),"yyyy-MM-dd hh:mm:ss"));
        System.out.println(DateUtil.dateToString(DateUtil.getDayBegin(), "yyyy-MM-dd hh:mm:ss"));
        System.out.println(DateUtil.dateToString(DateUtil.getDayEnd(), "yyyy-MM-dd hh:mm:ss"));
        System.out.println(DateUtil.getBeginDayOfMonth());
        System.out.println(DateUtil.getEndDayOfMonth());
        System.out.println(DateUtil.getBeginDayOfWeek());
        System.out.println(DateUtil.getEndDayOfWeek());
    }



	private static final Logger logger= LoggerFactory.getLogger(DateUtil.class);
    
	/**
     * 按YYYY-MM-DD HH:MM:SS格式返回今天的日期.
     * @return String
     */
    public static String getToday() { 
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //规定日期格式
        Date date = new Date();                 //将符合格式的String转换为Date
        String today = formatter.format(date);  //将Date转换为符合格式的String
        return today;
    }

    /**
     * 计算出与指定时间papaDay n天之后的时间
     * 
     * @return
     * @throws ParseException
     */
    public static String getAddDay(String papaDay, int num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calender = Calendar.getInstance();
        try {
            calender.setTime(sdf.parse(papaDay));
            calender.add(Calendar.DATE, num);
            return sdf.format(calender.getTime());
        } catch (ParseException e) {
            return null;
        }
    }
    
    /**
     * 获取当前时间
     * @return
     */
    public static String getToday5() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); // 规定日期格式
        Date date = new Date(); // 将符合格式的String转换为Date
        String today = formatter.format(date); // 将Date转换为符合格式的String
        return today;
    }
    
    /**
     * 计算出与指定时间papaDay n月之后的时间
     * 
     * @return
     * @throws ParseException
     */
    public static String getAddMonth(String papaDay, int num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calender = Calendar.getInstance();
        try {
            calender.setTime(sdf.parse(papaDay));
            calender.add(Calendar.MONTH, num);
            return sdf.format(calender.getTime());
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 计算出与指定时间papaDay n年之后的时间
     * 
     * @return
     * @throws ParseException
     */
    public static String getAddYear(String papaDay, int num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calender = Calendar.getInstance();
        try {
            calender.setTime(sdf.parse(papaDay));
            calender.add(Calendar.YEAR, num);
            return sdf.format(calender.getTime());
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 获取当前时间
     * @return
     */
    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); 
        Date date = new Date(); 
        String today = formatter.format(date);
        return today;
    }
    
    /**
     * 按yyyy-MM-dd格式返回今天的日期.
     * @return String
     */
    public static String getToday1() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
        Date date = new Date();  
        String today = formatter.format(date);  
        return today;
    }

    /**
     * 按yyyy年MM月dd日HH时mm分ss秒格式返回今天的日期.
     * 
     * @return String
     */
    public static String getToday2() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); 
        Date date = new Date(); 
        String today = formatter.format(date); 
        return today;
    }

    /**
     * 按yyyyMMddHHmmss格式返回今天的日期.
     * 
     * @return String
     */
    public static String getToday3() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); 
        Date date = new Date(); 
        String today = formatter.format(date); 
        return today;
    }

    /**
     * 按yyyyMMddHHmmsssss格式返回今天的日期.
     * 
     * @return String
     */
    public static String getToday4() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");  
        Date date = new Date();  
        String today = formatter.format(date);  
        return today;
    }

    /**
     * 返回今天的年份.
     * 
     * @return String
     */
    public static String getYear() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");  
        Date date = new Date();  
        String today = formatter.format(date); 
        return today;
    }

    /**
     * 返回今天的月份.
     * 
     * @return String
     */
    public static String getMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM"); 
        Date date = new Date(); 
        String today = formatter.format(date);  
        return today;
    }

    /**
     * 返回今天的日子数.
     * @return String
     */
    public static String getDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        Date date = new Date();
        String today = formatter.format(date);
        return today;
    }
    
    /**
     * 根据给定的日期获取改月的第一天或者最后一天
     * @author zxs 
     * @param theday 格式为 2009-01-03 
     * @param type 1 返回当月第一天 2 返回当月最后一天
     * @return
     */
    public static String getDayOfMonth(String theday,int type){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calender = Calendar.getInstance();
        if (type < 1 || type > 2 ) {
            return null;
        }
        try {
            calender.setTime(sdf.parse(theday));
            calender.set(Calendar.DATE, 1);             
            if (type == 2) {
                calender.roll(Calendar.DATE, -1);               
            }
            return sdf.format(calender.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getTodayToString() {
        return getYear() + getMonth() + getDay();

    }

    /**
     * 计算给定的两个日期相差的天数:end - start
     * @param start
     * @param end
     * @return
     */
    public static long getDiffNum(String start,String end){
         long quot = 0;
          SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
          try {
           Date d_start = ft.parse( start );
           Date d_end = ft.parse( end );
           quot = d_end.getTime() - d_start.getTime();
           quot = quot / 1000 / 60 / 60 / 24;
          } catch (ParseException e) {
           e.printStackTrace();
          }
          return quot;
    }
    
    /**
     * 计算给定的两个时间相差的秒数:end - start
     * @param start
     * @param end
     * @return
     */
    public static long getDiffNumBySec(String start,String end){
         long quot = 0;
          SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          try {
           Date d_start = ft.parse( start );
           Date d_end = ft.parse( end );
           quot = d_end.getTime() - d_start.getTime();
           quot = quot / 1000 ;
          } catch (ParseException e) {
           e.printStackTrace();
          }
          return Math.abs(quot);
    }        
	
	/**
	 * 当前时间转到时间String
	 * @param format
	 * @return
	 */
	public static String getNowToString(String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String sNowTime = sdf.format(date);
		return sNowTime;
	}

	/**
	 * 当前时间转到时间Date
	 * @param format
	 * @return
	 */
	public static Date getNowToDate(String format) {
		String date = getNowToString("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date sNowTime = sdf.parse(date);
			return sNowTime;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * date转到时间stirng
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String sNowTime = sdf.format(date);
		return sNowTime;
	}

	/**
	 * 时间stirng转到date
	 * @param str
	 * @param format
	 * @return
	 */
	public static Date stringToDate(String str, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * long转换成时间格式的String
	 * @param longTime
	 * @param format
	 * @return
	 */
	public static String longToString(long longTime, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String date = null;
		try {
			date = sdf.format(longTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	
	
	// date转到时间stirng
	public static String getNowDateString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String sNowTime = sdf.format(date);
		return sNowTime;

	}
	//获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
  //获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }
    //获取本周的结束时间
    public static Date getEndDayOfWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());  
        cal.add(Calendar.DAY_OF_WEEK, 6); 
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }
    //获取本月的开始时间
     public static Date getBeginDayOfMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getNowYear(), getNowMonth() - 1, 1);
            return getDayStartTime(calendar.getTime());
        }
    //获取本月的结束时间
     public static Date getEndDayOfMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getNowYear(), getNowMonth() - 1, 1);
            int day = calendar.getActualMaximum(5);
            calendar.set(getNowYear(), getNowMonth() - 1, day);
            return getDayEndTime(calendar.getTime());
        }
     	//获取某个日期的开始时间
	    public static Date getDayStartTime(Date d) {
	        Calendar calendar = Calendar.getInstance();
	        if(null != d) {
                calendar.setTime(d);
            }
	        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
	        calendar.set(Calendar.MILLISECOND, 0);
	        return new Date(calendar.getTimeInMillis());
	    }
	    //获取某个日期的结束时间
	    public static Date getDayEndTime(Date d) {
	        Calendar calendar = Calendar.getInstance();
	        if(null != d) {
                calendar.setTime(d);
            }
	        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
	        calendar.set(Calendar.MILLISECOND, 999);
	        return new Date(calendar.getTimeInMillis());
	    }
	    //获取今年是哪一年
	     public static Integer getNowYear() {
	             Date date = new Date();
	            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
	            gc.setTime(date);
	            return Integer.valueOf(gc.get(1));
	        }
	     //获取本月是哪一月
	     public static int getNowMonth() {
	             Date date = new Date();
	            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
	            gc.setTime(date);
	            return gc.get(2) + 1;
	        }
	    
	     /**
		     * @Method: getDate
		     * @Description: 获取提前或延后的日期
		     * @param dateStr
		     * @param hour
		     * @return 
		     * @return String
		     */
		     public static String getDate(String dateStr, int hour) {
		    	 try {
			    	 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    	 Date date = format.parse(dateStr);
			    	 Calendar cal = Calendar.getInstance();
			    	 cal.setTime(date);
			    	 cal.add(Calendar.HOUR_OF_DAY, hour);
			    	 return format.format(cal.getTime());
		    	 } catch (Exception e) {
		    	     return "";
		    	 }
		     }



	     /** 
	      * 获得指定日期的前一天 
	      * @param specifiedDay 
	      * @return 
	      * @throws Exception 
	      */ 
	      public static String getSpecifiedDayBefore(String specifiedDay){ 
	      //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
	      Calendar c = Calendar.getInstance(); 
	      Date date=null; 
	      try { 
	      date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); 
	      } catch (ParseException e) { 
	      e.printStackTrace(); 
	      } 
	      c.setTime(date); 
	      int day=c.get(Calendar.DATE); 
	      c.set(Calendar.DATE,day-1); 

	      String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); 
	      return dayBefore; 
	      } 
	      /** 
	      * 获得指定日期的后一天 
	      * @param specifiedDay 
	      * @return 
	      */ 
	      public static String getSpecifiedDayAfter(String specifiedDay){ 
	      Calendar c = Calendar.getInstance(); 
	      Date date=null; 
	      try { 
	      date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); 
	      } catch (ParseException e) { 
	      e.printStackTrace(); 
	      } 
	      c.setTime(date); 
	      int day=c.get(Calendar.DATE); 
	      c.set(Calendar.DATE,day+1); 

	      String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); 
	      return dayAfter; 
	      } 
	    
}
